package com.yjx.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.yjx.dto.ResourceDto;
import com.yjx.entity.Resource;
import com.yjx.dao.ResourceMapper;
import com.yjx.service.CacheService;
import com.yjx.service.ResourceService;
import com.yjx.service.UserTokenService;
import com.yjx.vo.ParentResourceVo;
import com.yjx.vo.UserVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (Resource)表服务实现类
 *
 * @author makejava
 * @since 2023-05-02 11:46:50
 */
@Service("resourceService")
public class ResourceServiceImpl implements ResourceService {
    @javax.annotation.Resource
    private ResourceMapper resourceMapper;
    @javax.annotation.Resource
    private UserTokenService userTokenService;
    @javax.annotation.Resource
    private CacheService cacheService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Resource queryById(Integer id) {
        return this.resourceMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Resource> queryAllByLimit(int offset, int limit) {
        return this.resourceMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param resource 实例对象
     * @return 实例对象
     */
    @Override
    public Resource insert(Resource resource) {
        this.resourceMapper.insert(resource);
        return resource;
    }

    /**
     * 修改数据
     *
     * @param resource 实例对象
     * @return 实例对象
     */
    @Override
    public Resource update(Resource resource) {
        this.resourceMapper.update(resource);
        return this.queryById(resource.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.resourceMapper.deleteById(id) > 0;
    }
    @Value("parentResource")
    private String parentResource;
    @Override
    public List<ParentResourceVo> queryAllResource() {
        //从缓存中获取所有权限
        List<ParentResourceVo> resourceVoList = cacheService.getResourceVoList(parentResource);
        if (resourceVoList==null){
            //从sql中查询
            System.out.println("从数据库中查询=======================================");
            resourceVoList = resourceMapper.queryAllResource();
            //存到缓存
            cacheService.saveResource(parentResource,resourceVoList);
        }
        System.out.println("从缓存中查询=======================================");
        return resourceVoList;
    }

    @Override
    public Integer saveResource(ResourceDto resourceDto,String token) {
        String userIson = userTokenService.getUserByToken(token);
        JsonMapper jsonMapper = new JsonMapper();
        Date date = new Date();
        try {
            UserVo userVo = jsonMapper.readValue(userIson, UserVo.class);
            resourceDto.setModifyUser(userVo.getId());
            resourceDto.setCreateTime(date);
            resourceDto.setModifyTime(date);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }

        return resourceMapper.saveResource(resourceDto);
    }
}

